package com.mz.mattress.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.mattress.admin.api.resp.IntegerCount;
import com.mz.mattress.admin.api.resp.MerchantVo;
import com.mz.mattress.api.entity.Merchant;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MerchantMapper extends BaseMapper<Merchant> {

//    @Select("select * from mz_merchant where mobile=#{mobile} and deleted=0")
//    List<Merchant> selectByMobile(@Param("mobile") String mobile);

    @Select("select count(*) from mz_merchant where member_id=#{memberId} and deleted=0")
    Integer selectCountByMemberId(@Param("memberId") Long memberId);

    //在申请中的 店铺
    @Select("select * from mz_merchant where member_id=#{memberId} and status in (1,2) and deleted=0")
    List<MerchantVo> selectApplyingByMemberId(@Param("memberId") Long memberId);

    @Select("select count(*) from mz_merchant where member_id=#{memberId} and status in (1,2) and deleted=0")
    Integer selectApplyingCountByMemberId(@Param("memberId") Long memberId);

    @Select("select * from mz_merchant where member_id=#{memberId} and status in (3,4,5) and deleted=0")
    List<MerchantVo> selecOpeningtList(@Param("memberId") Long memberId);

    @Select("select * from mz_merchant where member_id=#{memberId} and status!=6 and deleted=0 limit 1")
    MerchantVo selectOne(@Param("memberId") Long memberId);

    Page<Merchant> listPage(Page page,
                            @Param("province") String province,
                            @Param("city") String city,
                            @Param("district") String district,
                            @Param("name") String name,
                            @Param("sale") String sale,
                            @Param("saleId") Integer saleId,
                            @Param("pid") Integer pid,
                            @Param("investor") String investor,
                            @Param("status") Integer status,
                            @Param("statusType") Integer statusType,
                            @Param("grade") Integer grade,
                            @Param("deviceCount") Integer deviceCount,
                            @Param("startTime") String startTime,
                            @Param("endTime") String endTime);

    List<Merchant> listExport(
            @Param("province") String province,
            @Param("city") String city,
            @Param("district") String district,
            @Param("name") String name,
            @Param("sale") String sale,
            @Param("saleId") Integer saleId,
            @Param("pid") Integer pid,
            @Param("investor") String investor,
            @Param("status") Integer status,
            @Param("statusType") Integer statusType,
            @Param("grade") Integer grade,
            @Param("deviceCount") Integer deviceCount,
            @Param("startTime") String startTime,
            @Param("endTime") String endTime);

    @Delete("delete from mz_merchant where mobile=#{mobile}")
    void deleteByMobile(@Param("mobile") String mobile);

    List<IntegerCount> selectSumByColumn(@Param("columnName") String columnName, @Param("columnValue") Integer columnValue);

    @Update("update mz_merchant set investor_id=null,investor_name=null,investor_mobile=null where id=#{id}")
    Integer cleanInvestor(@Param("id") Integer id);

    @Update("UPDATE mz_merchant a,mz_device b set b.investor_id=null WHERE a.id=b.merchant_id and a.id=#{id}")
    Integer cleanDeviceInvestor(@Param("id") Integer id);

    @Select("select * from mz_merchant where !isnull(sale_mobile) and isnull(sale_id) and deleted=0")
    List<Merchant> list2();

    @Update("update mz_merchant set sale_mobile=#{saleMobile} where sale_id=#{saleId}")
    Integer updateSaleMobile(@Param("saleId") Integer saleId, @Param("saleMobile") String saleMobile);

    //绑定全部投资商
    @Update("UPDATE mz_merchant a,mz_device b set b.investor_id=a.investor_id WHERE a.id=b.merchant_id ")
    Integer bindDeviceInvestor();

    @Update("update mz_merchant set top_sale_id=null,sale_id=null,sale_mobile=null,sale_name=null,investor_id=null,investor_mobile=null,investor_name=null,deleted=1 where id=#{id}")
    Integer deletedMerchant(@Param("id") Integer id);

    List<MerchantVo> selectByIds(@Param("ids") List<Integer> ids);

    @Update("update mz_merchant set device_count=0")
    Integer cleanDeviceCount();

}
